package kr.ac.jejuuniv.repository;

import java.util.List;

import kr.ac.jejuuniv.model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

public class UserRepositoryImpl implements UserRepository {
	
	@Autowired
	private JdbcDaoSupport jdbcDaoSupport;

	@Autowired
	private RowMapper<User> rowMapper;
	
	

	public List<User> findAll() {
		return jdbcDaoSupport.getJdbcTemplate().query("select * from users", rowMapper);
	}



	public void insert(User user) {
		jdbcDaoSupport.getJdbcTemplate().update("insert into users (id, name, password) values (?, ?, ?)", user.getId(), user.getName(), user.getPassword());
	}



	public User findById(String id) {
		try {
			return jdbcDaoSupport.getJdbcTemplate().queryForObject("select * from users where id = ?", rowMapper, id);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}



	public void update(User user) {
		jdbcDaoSupport.getJdbcTemplate().update("update users set name = ?, password = ? where id = ?", user.getName(), user.getPassword(), user.getId());
	}



	public void delete(String id) {
		jdbcDaoSupport.getJdbcTemplate().update("delete from users where id = ?", id);
		
	}

}
